package com.geek.novel.repository

import android.text.TextUtils
import com.geek.novel.common.Constant
import com.geek.novel.common.Constant.SERVICE_FILE
import com.geek.novel.common.Constant.SERVICE_NOVEL
import com.geek.novel.entity.SysUser
import com.geek.novel.utils.CommUtil
import com.geek.novel.utils.SPUtil
import com.google.gson.JsonObject
import io.reactivex.Observable
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.*

interface WebRepository{
	
	companion object {
		
		var token: String? = null
		
		private var client: OkHttpClient.Builder = OkHttpClient.Builder()
		
		private var webRepository: WebRepository? = null

		/**
		 * 创建一个服务对象
		 */
		fun create(): WebRepository {
			client.addInterceptor{ chain ->
				var request = chain.request()
				val requestBuilder = request.newBuilder()
					.addHeader("User-Agent", "novel/android")
					.addHeader("platform", "Android")
					.addHeader("Content-Type", "application/json")
				if (!CommUtil.isNull(token)) {
					requestBuilder.addHeader("Authorization", "Bearer $token")
				}
				request = requestBuilder.build()
				chain.proceed(request)
			}
//				.addInterceptor(TokenInterceptor())
			if(webRepository == null) {
				createWebRepositoryBaseUrl()
			}
			return webRepository as WebRepository
		}
		
		/**
		 * 动态修改baseUrl
		 **/
		private fun createWebRepositoryBaseUrl(){
			var curIpUrl = SPUtil[SPUtil.updateServiceIp, "", SPUtil.SP_UPDATE_HTTP_SERVICE_IP] as String
			if(TextUtils.isEmpty(curIpUrl.toString())){
				 curIpUrl = Constant.HTTP_SERVICE_IP
			}
			val builder = Retrofit.Builder()
					.client(client.build())
					.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
					.addConverterFactory(GsonConverterFactory.create())//解析json
					.addConverterFactory(ScalarsConverterFactory.create())//解析String
					.baseUrl(curIpUrl)
			webRepository = builder.build().create(WebRepository::class.java)
		}
	}
	
	/***************************************************clientCdzService***************************************/

	/**
	 * 登录接口
	 */
	@POST("${SERVICE_NOVEL}/auth/login")
	fun login(@Body map: Map<String, String>): Observable<JsonObject>

	/**
	 * 获取用户信息接口
	 */
	@POST("${SERVICE_NOVEL}/auth/getUserInfo")
	fun getUserInfo(): Observable<JsonObject>

	/**
	 * 注册接口
	 */
	@POST("${SERVICE_NOVEL}/auth/regist")
	fun regist(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 更新用户信息接口
	 */
	@POST("${SERVICE_NOVEL}/auth/user/save")
	fun updateUserInfo(@Body user: SysUser): Observable<JsonObject>

	/**
	 * 修改密码接口
	 */
	@POST("${SERVICE_NOVEL}/auth/modifyPassword")
	fun modifyPassword(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 发送忘记密码的验证码
	 */
	@POST("${SERVICE_NOVEL}/auth/sendForgetPassValidateCode")
	fun sendForgetPassValidateCode(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 忘记密码-修改密码
	 */
	@POST("${SERVICE_NOVEL}/auth/forgetPassword")
	fun forgetPassword(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 上传图片
	 */
	@Multipart
	@POST("${SERVICE_FILE}/file/uploadFile")
	fun uploadFile(@Part body: MultipartBody.Part, @Query("fileType") fileType: String, @Query("fileId") fileId: Long?, @Query("fileName") fileName: String?, @Query("filePath") filePath: String?): Observable<JsonObject>

	/**
	 * 获取图片
	 */
	@POST("${SERVICE_FILE}/file/image")
	fun downloadImage(@Query("id") id: Long): Observable<ResponseBody>

	/**
	 * 检查更新
	 */
	@POST("${SERVICE_NOVEL}/novel/app/version/check")
	fun checkUpdate(@Body map: Map<String, String>): Observable<JsonObject>

	/**
	 * 获取字体列表
	 */
	@POST("${SERVICE_NOVEL}/novel/app/typeface/all")
	fun getTypefaceList(): Observable<JsonObject>

	/**
	 * 获取首页推荐小说
	 */
	@GET("${SERVICE_NOVEL}/novel/store/home")
	fun getRecommendBooks(): Observable<JsonObject>

	/**
	 * 获取书籍详情接口
	 */
	@POST("${SERVICE_NOVEL}/novel/store/bookDetail")
	fun bookDetail(@Body map: Map<String, String>): Observable<JsonObject>

	/**
	 * 获取书籍详情接口
	 */
	@POST("${SERVICE_NOVEL}/novel/store/authorBooks")
	fun getAuthorBooks(@Body map: Map<String, String>): Observable<JsonObject>

	/**
	 * 根据关键字搜索书籍信息
	 */
	@POST("${SERVICE_NOVEL}/novel/store/bookSearch")
	fun bookSearch(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 获取所有爬虫源
	 */
	@GET("${SERVICE_NOVEL}/novel/store/crawlerSourceList")
	fun getCrawlerSourceList(): Observable<JsonObject>

	/**
	 * 获取小说分类
	 */
	@POST("${SERVICE_NOVEL}/novel/store/bookCategories")
	fun bookCategories(): Observable<JsonObject>

	/**
	 * 根据分类信息获取小说列表
	 */
	@POST("${SERVICE_NOVEL}/novel/store/queryBookByCategory")
	fun queryBookByCategory(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 根据bookId获取爬虫源信息
	 */
	@POST("${SERVICE_NOVEL}/novel/store/bookCrawlerList")
	fun getBookCrawlerList(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 用户加收藏
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/add")
	fun addUserCollect(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 用户删除收藏
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/remove")
	fun removeUserCollect(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 获取用户收藏列表
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/query")
	fun queryUserCollected(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 书籍点击数+1
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/addVisitCount")
	fun addVisitCount(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 书籍阅读数+1
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/addReadCount")
	fun addReadCount(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 书籍阅读数-1
	 */
	@POST("${SERVICE_NOVEL}/novel/user/collect/minusReadCount")
	fun minusReadCount(@Body map: JsonObject): Observable<JsonObject>

	/**
	 * 热搜词
	 */
	@POST("${SERVICE_NOVEL}/novel/store/bookSearchHotKey")
	fun queryBookSearchHotKey(): Observable<JsonObject>

}